package com.code2roc.jarmanage.servlet.firewall;

import com.code2roc.jarmanage.db.FireWallRuleDB;
import com.code2roc.jarmanage.firewall.FireWallService;
import com.code2roc.jarmanage.firewall.FireWallServiceFactory;
import com.code2roc.jarmanage.model.FireWallRuleDO;
import com.code2roc.jarmanage.model.Result;
import com.code2roc.jarmanage.util.CommonUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class FireWallInsertServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Result result = Result.okResult();
        FireWallRuleDO entity = CommonUtil.getParamEntity(req, FireWallRuleDO.class);
        FireWallService service = new FireWallServiceFactory().CreateFireWallService();
        if(service.addRule(entity.getRuleName(),entity.getRuleProtocol(),entity.getRulePort(),entity.getRuleAction())){
            FireWallRuleDB.insert(entity);
        }else{
            result = Result.errorResult();
        }
        resp.getWriter().println(result.toString());
    }
}
